#******************************************************************
#
# Copyright 2015 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

##
# ResourceClient Unit Test build script
##
# SConscript file for Local PKI google tests

import os
from tools.scons.RunTest import run_test

gtest_env = SConscript('#extlibs/gtest/SConscript')
lib_env = gtest_env.Clone()

target_os = lib_env.get('TARGET_OS')

if lib_env.get('RELEASE'):
    lib_env.AppendUnique(CCFLAGS=['-Os'])
else:
    lib_env.AppendUnique(CCFLAGS=['-g'])

# Add third party libraries
SConscript('#service/third_party_libs.scons', 'lib_env')

if target_os in ['linux']:
    SConscript('#extlibs/hippomocks/SConscript')

rcs_test_env = lib_env.Clone()

######################################################################
#unit test setting
######################################################################
src_dir = lib_env.get('SRC_DIR')

######################################################################
# Build flags
######################################################################

rcs_test_env.AppendUnique(CPPPATH=[
    '#/extlibs/hippomocks/hippomocks',
    '../include',
    '../src/common/utils/include',
    '#/resource/c_common',
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/include',
    '#/resource/oc_logger/include',
    '#/resource/csdk/connectivity/api'
])

rcs_test_env.PrependUnique(LIBS=[
    'connectivity_abstraction',
    'oc_logger',
    'oc_logger_core',
    'octbstack',
    'oc',
    'rcs_client',
    'rcs_server',
    'rcs_common',
])

if lib_env.get('SECURED') == '1':
    rcs_test_env.AppendUnique(CPPDEFINES=['SECURED'])
    rcs_test_env.AppendUnique(LIBS=['mbedtls', 'mbedx509', 'mbedcrypto'])

if 'g++' in rcs_test_env.get('CXX'):
    rcs_test_env.AppendUnique(CXXFLAGS=['-std=c++0x'])

######################################################################
# Build Test
######################################################################
rcs_test_src = rcs_test_env.Glob('./*.cpp')
unittests = []
rcs_client_test = rcs_test_env.Program('rcs_client_test', rcs_test_src)
Alias("rcs_client_test", rcs_client_test)
rcs_test_env.AppendTarget('rcs_client_test')
unittests += rcs_client_test;
unittests += rcs_test_env.ScanJSON('service/resource-encapsulation/unittests')
rcs_test_env.Alias("install", unittests)
rcs_test_env.UserInstallTargetExtra(unittests,'tests/service/resource-encapsulation/')

if rcs_test_env.get('TEST') == '1':
    if target_os in ['linux']:
        rcs_test_env.AppendUnique(CPPDEFINES=['HIPPOMOCKS_ISSUE'])
        run_test(
            rcs_test_env,
            'service_resource-encapsulation_unittests_rcs_client_test.memcheck',
            'service/resource-encapsulation/unittests/rcs_client_test')
else:
    rcs_test_env.AppendUnique(CPPDEFINES=['LOCAL_RUNNING'])
